/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package userinterface;

import costoptiontree.CostOption;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JSlider;

/**
 *
 * @author Michael
 */
public class CostOptionView extends javax.swing.JPanel implements ActionListener{
    
    private CostOption option;
    private boolean isOptionSet = false;
    private JButton button_dummyCostChanged;
    
    public static final String ACTION_COST_CHANGED = "optionCostChanged";
    /**
     * Creates new form CostOptionView
     */
    public CostOptionView() {
        initComponents();
        jTextArea_description.setLineWrap(true);
        
        button_dummyCostChanged = new JButton();
        button_dummyCostChanged.setVisible(false);
        button_dummyCostChanged.setActionCommand(CostOptionView.ACTION_COST_CHANGED);
        
    }
    
    public void addCostChangedListener(ActionListener listener){
        button_dummyCostChanged.addActionListener(listener);
    }

    public CostOption getOption() {
        return option;
    }

    public void setOption(CostOption option) {
        this.option = option;
        isOptionSet = true;
        
        jLabel_label.setText(option.getLabel());
        jTextArea_description.append(option.getDescription());
        
        refresh();
        
        jSlider_cost.setMaximum(option.getMaxCost());
        jSlider_cost.setMinimum(option.getMinCost());
        
        jSlider_cost.setValue(option.getSelectedCost());
        
        jLabel_unitMax.setText(new Integer(option.getMaxCost()).toString());
        jLabel_unitMin.setText(new Integer(option.getMinCost()).toString());
        
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel_unitMax = new javax.swing.JLabel();
        jLabel_unitSelected = new javax.swing.JLabel();
        jLabel_scaled = new javax.swing.JLabel();
        jLabel_scaledMin = new javax.swing.JLabel();
        jSlider_cost = new javax.swing.JSlider();
        jLabel_unit = new javax.swing.JLabel();
        jLabel_scaledSelected = new javax.swing.JLabel();
        jLabel_scaledMax = new javax.swing.JLabel();
        jLabel_unitMin = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane_descriptionScrollPane = new javax.swing.JScrollPane();
        jTextArea_description = new javax.swing.JTextArea();
        jLabel_label = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel_unitMax.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_unitMax.setText("unitMax");

        jLabel_unitSelected.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_unitSelected.setText("unitSelected");

        jLabel_scaled.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel_scaled.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_scaled.setText("$,scaled");

        jLabel_scaledMin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_scaledMin.setText("scaledMin");

        jSlider_cost.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider_costStateChanged(evt);
            }
        });

        jLabel_unit.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel_unit.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_unit.setText("$,unit");

        jLabel_scaledSelected.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_scaledSelected.setText("scaledSelected");

        jLabel_scaledMax.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_scaledMax.setText("scaledMax");

        jLabel_unitMin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_unitMin.setText("unitMin");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel_unitMin)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel_unitSelected, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel_unitMax))
                    .addComponent(jSlider_cost, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel_scaledMin)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel_scaledSelected, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel_scaledMax, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_scaled, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_unit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_scaledMin)
                    .addComponent(jLabel_scaledMax)
                    .addComponent(jLabel_scaledSelected)
                    .addComponent(jLabel_scaled))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSlider_cost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_unitMin)
                    .addComponent(jLabel_unitMax)
                    .addComponent(jLabel_unitSelected)
                    .addComponent(jLabel_unit))
                .addGap(2, 2, 2))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jTextArea_description.setEditable(false);
        jTextArea_description.setColumns(20);
        jTextArea_description.setLineWrap(true);
        jTextArea_description.setRows(5);
        jTextArea_description.setWrapStyleWord(true);
        jScrollPane_descriptionScrollPane.setViewportView(jTextArea_description);

        jLabel_label.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel_label.setText("label");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_label, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane_descriptionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jScrollPane_descriptionScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_label, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    private void jSlider_costStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider_costStateChanged
        int value = jSlider_cost.getValue();

        JSlider source = (JSlider)evt.getSource();
        if (!source.getValueIsAdjusting()) {
            int selectedCost = (int)source.getValue();
            
            if (isOptionSet){
                option.setSelectedCost(selectedCost);
                jLabel_unitSelected.setText(new Integer(option.getSelectedCost()).toString());
                refresh();
            }
            button_dummyCostChanged.doClick();
        }
        
        
    }//GEN-LAST:event_jSlider_costStateChanged

    @Override
    public void actionPerformed(ActionEvent evt) {
        System.out.println("actionPerformed in CostOptionView: "+evt.getActionCommand());
        if (!isOptionSet){
            return;
        }

        if (BaseComponentListWithDeleteButtons.actionDeleteComponent.equals(evt.getActionCommand())) {
            System.out.println("deleting option");
            delete();
        } 
    }
    
    public void delete(){
        
        if (option.getParent()==null){
            return;
        }
        option.getParent().removeCostOption(option);
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel_label;
    private javax.swing.JLabel jLabel_scaled;
    private javax.swing.JLabel jLabel_scaledMax;
    private javax.swing.JLabel jLabel_scaledMin;
    private javax.swing.JLabel jLabel_scaledSelected;
    private javax.swing.JLabel jLabel_unit;
    private javax.swing.JLabel jLabel_unitMax;
    private javax.swing.JLabel jLabel_unitMin;
    private javax.swing.JLabel jLabel_unitSelected;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane_descriptionScrollPane;
    private javax.swing.JSlider jSlider_cost;
    private javax.swing.JTextArea jTextArea_description;
    // End of variables declaration//GEN-END:variables

    void refresh() {
        jLabel_scaledMax.setText(new Integer(option.getScaledMaxCost()).toString());
        jLabel_scaledMin.setText(new Integer(option.getScaledMinCost()).toString());
        jLabel_scaledSelected.setText(new Integer(option.getScaledCost()).toString());
    }
}
